function userDate(uData, type = 1) {
    var myDate = new Date(uData * 1000);
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var day = myDate.getDate();
    var hours = myDate.getHours();
    var minutes = myDate.getMinutes();
    switch (type) {
        case 1:
            return year + '年' + month + '月' + day + "日" + hours + ":" + minutes;
            break;
        case 2:
            return year + '年' + month + '月' + day + "日";
            break;


    }




}

function fnSiteUrl(value) {
    return SITE_URL + value;
}

function fnclock2() {

    $(".time5").each(function() {
        var EndTime = $(this).find(".timeNum").val();
        //alert(EndTime);

        var NowTime = parseInt(EndTime - 1);
        if (NowTime > 0) {
            $(this).find(".timeNum").val(NowTime);
            var E_time = userDate1(NowTime);

            $(this).find("span").html(E_time);
        } else {
            clearTimeout(fts);
        }


    });

}

function userDate1(uData) {
    var M = parseInt(uData % 60);
    var F = parseInt(uData / 60 % 60);
    var S = parseInt(uData / 60 / 60 % 24);
    var T = parseInt(uData / 60 / 60 / 24);
    //var S=uData/60/60/24;
    //return uData;
    return T + '天' + S + '时' + F + "分" + M + "秒";
}
//关闭，并且刷新指定的网页
function fncloseWinCommon(winName, frameName, fn) {
    api.execScript({
        name: winName,
        frameName: frameName,
        script: fn
    });
    api.closeWin({});
}
//打开一个window 点击，获取当前节点上面 data-url属性 data-page-param 给下一个页面传递的属性
function fnOpenWinCommon(that, pageParam = {
    test: "test"
}) {
    var data_url = $(that).attr("data-url");


    api.openWin({
        name: data_url,
        url: './' + data_url + '.html',
        pageParam: pageParam
    });

}
//退出登陆
function fnLoginOutCommon() {

    api.confirm({
        title: '提示：',
        msg: '您确定退出？',
        buttons: ['确定', '取消']
    }, function(ret, err) {
        var index = ret.buttonIndex;
        if (index == 1) {
            $api.clearStorage();
            api.openWin({
                name: 'main',
                url: './main.html',
                opaque: true,
                cScrollBarEnabled: false,
                reload: true,

            });
        }
    });



}
//获取数据函数
function fnGetDataCommon(template_, url_, method_, data_ = {}, userinfo_ = 1) {
    api.showProgress({//添加加载中
        title: '努力加载中...',
        text: '先喝杯茶...',
        modal: false
    });
    if (userinfo_ == 1) {
        var userInfo = $api.getStorage('userInfo');
        if (typeof(userInfo) == "undefined") {
            api.openWin({
                name: "login",
                url: "login.html",
                opaque: true,
                vScrollBarEnabled: false
            });
            return false;

        }
        //获取数据app中存入的数据然后分配到模板中；
        data_.userid = userInfo.userid;
        data_.random = userInfo.random;
    }

    api.ajax({
        url: SITE_URL + url_,
        method: method_,
        data: {
            values: data_
        }
    }, function(ret, err) {
        //alert(JSON.stringify(ret));
        if (ret.status == 1) {
            eval(template_ + 'Res(ret)');

        } else if (ret.status == 2) {
            api.openWin({
                name: 'login',
                url: './login.html',
                pageParam: {
                    name: 'test'
                }
            });

        } else if (ret.status == 5) {
            eval(template_ + 'Res1(ret)');
        } else {

            api.toast({
                msg: ret.msg,
                duration: 2000,
                location: 'middle'
            });

        }
        api.hideProgress();//隐藏加载中
    })
}
//下拉刷新公共
function initRefreshCommon(fnGet_="") {

    api.setRefreshHeaderInfo({
        visible: true,
        loadingImg: 'widget://image/refresh.png',
        bgColor: '#ccc',
        textColor: '#fff',
        textDown: '下拉刷新...',
        textUp: '松开刷新...',
        showTime: true
    }, function(ret, err) {
        eval(fnGet_);
        api.refreshHeaderLoadDone();
    });
}






//图片



//上传头像 图片的上传
function C_imgUpload(template_) {
    api.actionSheet({
        title: '上传照片',
        cancelTitle: '取消',
        buttons: ['拍照', '手机相册']
    }, function(ret, err) {
        if (ret) {
            C_getPicture(ret.buttonIndex, template_);
        }
    });
}

function C_getPicture(sourceType, template_) {
    switch (sourceType) {
        case 1:
            var operate_type = 'camera';
            break;
        case 2:
            var operate_type = 'album';
            break;

    }
    //获取一张图片
    api.getPicture({
        sourceType: operate_type, //
        encodingType: 'jpg',
        mediaValue: 'all',
        destinationType: 'base64',
        allowEdit: false,
        quality: 80,

        saveToPhotoAlbum: false
    }, function(ret, err) {
        if (ret.data) {
            //saveImg(ret.data,valueId,imgId);
            C_saveImg(ret.data, template_);
            //$api.byId('avatar').src=ret.data;
        } else {
            api.toast({
                msg: '图像获取失败',
                duration: 3000,
                location: 'bottom'
            })
        }
    });
}

function C_saveImg(path, template_) {
    api.showProgress({
        title: '上传中...',
        text: '先喝杯茶...',
    });
    //上传剪辑后的图像到服务器
    api.ajax({
        // report : false,
        url: SITE_URL + 'index.php?m=api&c=appnew&a=ajax_img_aliyun',
        //这里是我们约定好的后台上传图片的位置 ，你可以根据你的需求来改
        method: 'post',
        cache: 'false',
        timeout: 30,
        dataTpye: 'json',
        data: {
            files: {
                file: path
            }
        }
    }, function(ret, err) {
        if (ret.status == 1) {
            api.hideProgress();
            eval(template_ + 'Img(ret)');
        }

    });
};
